package com.jumi.microservice.controller.order.admin;

import com.alibaba.excel.EasyExcel;
import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.ExportExcelCommonResponse;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.page.TableDataInfo;
import com.jumi.microservice.dto.admin.*;
import com.jumi.microservice.dto.excel.ImportNoSelfResponse;
import com.jumi.microservice.dto.order.ChangeOrderTypeRequest;
import com.jumi.microservice.dto.order.KefuOrderRequest;
import com.jumi.microservice.dto.order.KefuOrderResponse;
import com.jumi.microservice.dto.order.OrderLogResponse;
import com.jumi.microservice.dto.wdt.PushOrderRequest;
import com.jumi.microservice.dto.wdt.WdtResultResponse;
import com.jumi.microservice.enumerate.OrderStatusEnum;
import com.jumi.microservice.mapper.UserOrderMapper;
import com.jumi.microservice.service.IOrderDubboService;
import com.jumi.microservice.service.order.AllOrderServiceImpl;
import com.jumi.microservice.service.order.ExportExcelServiceImpl;
import com.jumi.microservice.wdt.WdtOrderClient;
import com.jumi.microservice.wdt.WdtService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangjingdao
 */
@RestController
@Api(tags = "全部订单")
@RequestMapping("/order")
public class OrderAllController extends BaseController {


    @Autowired
    AllOrderServiceImpl service;

    @DubboReference
    private IOrderDubboService orderDubboService;

    @Resource
    UserOrderMapper userOrderMapper;

    @Resource
    private WdtService wdtService;

    @Resource
    ExportExcelServiceImpl exportExcelService;

    @Resource
    private WdtOrderClient wdtOrderClient;

    @PostMapping("/getOrderAll")
    @ApiOperation("订单查询")
    public TableDataInfo<OrderAllResponse> getOrderAll(FindOrderRequest request) {
        return service.getOrderAll(request);
    }

    @PostMapping("/goodsOrderAll")
    @ApiOperation("所有订单")
    public TableDataInfo<OrderGoodsAllResponse> goodsOrderAll(OrderGoodsRequest request) {
        return service.goodsOrderAll(request);
    }

    @PostMapping("/getOrderDetails")
    @ApiOperation("订单详情")
    private ResponseResult<OrderDetailsResponse> getOrderDetails(OrderDetailsRequest request) throws Exception {
        ResponseResult<OrderDetailsResponse> responseResponseResult = new ResponseResult<>();
        responseResponseResult.setData(service.getOrderDetails(request));
        return responseResponseResult;
    }

    @PostMapping("/getNonSelfOperated")
    @ApiOperation("非自营代发货")
    public TableDataInfo<NonSelfOperatedListResponse> getNonSelfOperated(NonSelfOperatedRequest request) {
        return service.getNonSelfOperated(request);
    }

    @PostMapping("/getPartial")
    @ApiOperation("部分发货")
    public TableDataInfo<PartialResponse> getPartial(PartialRequest request) {
        return service.getPartial(request);
    }

    @PostMapping("/getGoodsReceived")
    @ApiOperation("待收货")
    public TableDataInfo<PartialResponse> getGoodsReceived(GoodsReceivedRequest request) {
        return service.getGoodsReceived(request);
    }

    @PostMapping("/getCompletedList")
    @ApiOperation("已完成")
    public TableDataInfo<CompletedResponse> getCompletedList(CompletedRequest request) {
        return service.getCompletedList(request);
    }

    @PostMapping("/getAfterSale")
    @ApiOperation("售后")
    public TableDataInfo<AfterSaleResponse> getAfterSale(CompletedRequest request) {
        return service.getAfterSale(request);
    }

    @PostMapping("/getPreSale")
    @ApiOperation("预售")
    public TableDataInfo<PreSaleResponse> getPreSale(PreSaleRequest request) {
        return service.getPreSale(request);
    }

    @PostMapping("/setRemark")
    @ApiOperation("添加备注")
    public ResponseResult<Boolean> setRemark(@RequestBody RemarkRequest remarkRequest) {
        return service.setRemark(remarkRequest);
    }

    @PostMapping("/getToBeReviewed")
    @ApiOperation("待审核")
    public TableDataInfo<ToBeReviewedResponse> getToBeReviewed(ToBeReviewedRequest reviewedRequest) {
        return service.getToBeReviewed(reviewedRequest);
    }

    @PostMapping("/getOrderGoodsList")
    @ApiOperation("待审核/审核按钮")
    public TableDataInfo<OrderGoodsListResponse> getOrderGoodsList(OrderNoRequest request) {
        return service.getOrderGoodsList(request);

    }

    @PostMapping("/order-type/change")
    @ApiOperation("预售单转正式")
    public ResponseResult<Boolean> changeOrderType(@RequestBody ChangeOrderTypeRequest request) {
        return success(orderDubboService.changeOrderType(request));
    }

    @PostMapping("/updateDaysReceipt")
    @ApiOperation("延长收货日期")
    public ResponseResult<Boolean> updateDaysReceipt(DaysReceiptRequest request) {
        Long uid = getUid();
        String name = getUserName();
        return service.updateDaysReceipt(request,uid,name);
    }

    @PostMapping("/order-status/get")
    @ApiOperation("获取订单状态字典")
    public ResponseResult<Map<Integer, String>> getOrderStatus() {
        Map<Integer, String> map = new HashMap<>();
        for (OrderStatusEnum orderStatusEnum : OrderStatusEnum.values()) {
            map.put(orderStatusEnum.getCode().intValue(), orderStatusEnum.getMessage());
        }
        return success(map);
    }

    @PostMapping("getImportList")
    @ApiOperation("海外商品")
    public TableDataInfo<OrderGoodsAllResponse> getImportList(OrderGoodsRequest request) {
        return service.getImportList(request);
    }

    @ApiOperation("海外商品导出")
    @PostMapping("/isImport/export")
    public void excelExport(HttpServletResponse response, OrderGoodsRequest request) throws IOException {
        ExportExcelCommonResponse<ImportNoSelfResponse> excelExportExportExcelCommonResponse = service.excelExport(request);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + excelExportExportExcelCommonResponse.getFileName());
        EasyExcel.write(response.getOutputStream(), ImportNoSelfResponse.class).sheet("sheet1").doWrite(excelExportExportExcelCommonResponse.getData());
    }

    @ApiOperation("海外商品导入")
    @PostMapping("/isImport/import")
    public ResponseResult<List<String>> excelImport(HttpServletRequest request) throws IOException {
        Long uid = getUid();
        String name = getUserName();
        return success(exportExcelService.excelImportNoSeal(request, uid, name));
    }

    @PostMapping("getPreSaleToActual")
    @ApiOperation("预售转实际列表")
    public TableDataInfo<PreSaleToActualResponse> getPreSaleToActual(PreSaleToActualRequest request) {
        return service.getPreSaleToActual(request);
    }

    @GetMapping("getReturnGoods")
    @ApiOperation("退货的商品列表")
    public ResponseResult<List<ReturnGoodsResponse>> getReturnGoods(Integer orderId) {
        return service.getReturnGoods(orderId);
    }

    @GetMapping("getInvoiceGoodsResponse")
    @ApiOperation("发票订单详情")
    public ResponseResult<InvoiceGoodsResponse> getInvoiceGoodsResponse(String orderNo) {
        return service.getInvoiceGoodsResponse(orderNo);
    }

    @PostMapping("approvedExamineRequest")
    @ApiOperation("待审核一件通过当前页面")
    public ResponseResult<Boolean> approvedExamineRequest(ApprovedExamineRequest request) {
        return service.approvedExamineRequest(request);
    }

//    @GetMapping("/selfSave")
//    public ResponseResult<BigDecimal> selfSave(Long uid) {
//        return success(userOrderMapper.selfSave(uid));
//    }

    @PostMapping("approvedExamineAll")
    @ApiOperation("审核一件通过所有")
    public ResponseResult<Boolean> approvedExamineAll() {
        return service.approvedExamineAll();
    }

    @PostMapping("updateOrderDetails")
    @ApiOperation("订单详情修改")
    public ResponseResult<Boolean> updateOrderDetails(UpdateOrderDetailsRequest request) {
        Long uid = getUid();
        String name = getUserName();
        return service.updateOrderDetails(request,uid,name);
    }

    @PostMapping("getPartialNew")
    @ApiOperation("部分发货新的")
    @Deprecated
    public TableDataInfo<PartialResponse> getPartialNew(PartialRequest request) {
        return service.getPartialNew(request);
    }

    @PostMapping("confirmedAudit")
    @ApiOperation("审核通过")
    public ResponseResult<Boolean> confirmedAudit(@RequestBody ConfirmedAuditRequest request) throws Exception {
        return service.confirmedAudit(request);
    }

    @PostMapping("/wdt/push-order")
    @ApiOperation("旺店通一键推单")
    public ResponseResult<WdtResultResponse> pushOrder(PushOrderRequest pushOrderRequest) {
        return success(wdtService.pushOrder(pushOrderRequest));
    }

    @PostMapping("/wdt/getLogisticsInfo")
    @ApiOperation("旺店通同步物流信息按钮")
    public ResponseResult<Boolean> wdtLogistics() {
        wdtOrderClient.logisticsSync();
        return success(true);
    }


    @GetMapping("/log/list")
    @ApiOperation("订单日志")
    public ResponseResult<List<OrderLogResponse>> getOrderLogList(String orderSn) {
        return success(orderDubboService.getOrderLogList(orderSn));
    }

    @GetMapping("/serviceChargeList")
    @ApiOperation("推广金额")
    public ResponseResult<List<DistributionResponse>> serviceChargeList(String orderNo) {
        return service.serviceChargeList(orderNo);
    }

    @GetMapping("/serviceAmountList")
    @ApiOperation("服务金额")
    public ResponseResult<List<DistributionResponse>> serviceAmountList(String orderNo) {
        return service.serviceAmountList(orderNo);
    }

    @GetMapping("/KefuOrderList")
    @ApiOperation("客服调用根据用户查询所有订单")
    public TableDataInfo<KefuOrderResponse> kefuOrderListByBuyerId(KefuOrderRequest request){
        return orderDubboService.kefuOrderListByBuyerId(request);
    }


}
